package com.geologyoa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import com.geologyoa.domain.Power;
import com.geologyoa.domain.RoleAndPow;
import com.geologyoa.service.SystemManager;
import com.geologyoa.service.SystemManagerImpl;

public class SearchFunctionServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
       doPost(request,response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SystemManager sys=new SystemManagerImpl();
		Integer roleid=Integer.parseInt(request.getParameter("ROLEID"));
		Integer functionid=Integer.parseInt(request.getParameter("FUNCTIONID"));
		List<Power> powerList=sys.SearchFunction(roleid, functionid);
		Document document=DocumentHelper.createDocument();
		if(powerList!=null)
		{
			Element fwlistElement =document.addElement("powerList");
			fwlistElement.addAttribute("size", (new StringBuilder(String.valueOf(powerList.size()))).toString());
			Power power;
			Element fwElement;
			for(Iterator iterator=powerList.iterator();iterator.hasNext();)
			{
				power = (Power)iterator.next();
				fwElement=fwlistElement.addElement("powerInfo");
				fwElement.addAttribute("ROLEFUNCTION", power.getRolefunction());
			}
		}
		StringWriter xmlout = new StringWriter();
		XMLWriter output = new XMLWriter(xmlout);
		output.write(document);
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Pragma", "No-cache");
		response.getWriter().write(xmlout.toString());
	}

}
